package com.yunfei.utils.http;
import java.util.Map;
import java.util.TreeMap;

import com.google.gson.Gson;
import com.yunfei.api.user.result.shuanlgu.DoubleVideoJsonRoot;
import com.yunfei.utils.Md5Util;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class CopyOfNgnixLiveUtil

{
	 
	public static JSONObject buildHttpClient(String postUrl) {
		JSONObject result = new JSONObject();
    	HttpClient client = null;
		GetMethod method = null;
		client = new HttpClient();
	 
		client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); 
		client.getHttpConnectionManager().getParams().setSoTimeout(10000);
	 
	    method = new GetMethod(postUrl);
	 
		 
		method.addRequestHeader("Content-Type","application/json;charset=utf-8");
		try {
			int code = client.executeMethod(method);
			if (code != HttpStatus.SC_OK) {
				return null;
			} else {
				JSONObject event = null;
				JSONObject eventContent = null;
				String eventType = null;
				String respText = method.getResponseBodyAsString();
				JSONObject rec  = JSONObject.fromObject(respText);
				return rec;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result;
		} 
		finally {
			if (method != null) {
				method.releaseConnection();
			}
			if (client != null) {
				client.getHttpConnectionManager().closeIdleConnections(1);
			}
		}
		
    	
    }
	 
	public static JSONObject buildHttpClient(String postUrl,TreeMap<String, Object>  paramMap) {
		JSONObject result = new JSONObject();
		JSONObject paras = new JSONObject();
    	HttpClient client = null;
    	PostMethod method = null;
		client = new HttpClient();
		method = new PostMethod(postUrl);
		client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); 
		client.getHttpConnectionManager().getParams().setSoTimeout(10000);
		Object[] key = paramMap.keySet().toArray();
		 
		HttpMethodParams params = new HttpMethodParams();
		HttpClientParams paramc = new HttpClientParams();
		for (int i = 0; i < key.length; i++) {
			// 拼接参数
			System.out.println(paramMap.get(key[i]));
			 
			params .setParameter(key[i].toString(), paramMap.get(key[i]).toString());
			 
		}
		method.addRequestHeader("Content-Type","application/json;charset=utf-8");
//		NameValuePair[] param = { new NameValuePair("startCity","杭州"),  
//                new NameValuePair("lastCity","沈阳"),  
//                new NameValuePair("userID",""),  
//                new NameValuePair("theDate","") } ;  
		  
	 
		if (paramMap != null) {

            NameValuePair[] nvp = new NameValuePair[paramMap.values().size()];

            int i = 0;

            for (Map.Entry<String, Object> entry: paramMap.entrySet()) {
            	paras.put(entry.getKey(), entry.getValue().toString());
                nvp[i++] = new NameValuePair(entry.getKey(), entry.getValue()

                    .toString());

            }
            method.setRequestBody(paras.toString());
        	//method.setRequestBody(nvp);
            

        }
	
	
		try {
			int code = client.executeMethod(method);
			if (code != HttpStatus.SC_OK) {
				return null;
			} else {
				JSONObject event = null;
				JSONObject eventContent = null;
				String eventType = null;
				String respText = method.getResponseBodyAsString();
			 
				JSONObject rec = JSONObject.fromObject(respText);
				return rec;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result;
		} 
		finally {
			if (method != null) {
				method.releaseConnection();
			}
			if (client != null) {
				client.getHttpConnectionManager().closeIdleConnections(1);
			}
		}
		
    	
    }
	public static String   getDoubleVideoUrl(String fileName) {
	   String result="";
		TreeMap<String, Object>  paramMap = new TreeMap();
		paramMap.put("UserName", "demo@cloudroom.com");
		paramMap.put("UserPswd", Md5Util.string2MD5("123456"));
		paramMap.put("fileName", fileName);
		String postUrl ="https://shuanglu.shassel.com:2726/api/netdisk/getFiles";
		JSONObject aa = buildHttpClient(postUrl ,   paramMap);
		DoubleVideoJsonRoot doubleVideoJsonRoot= new Gson().fromJson(aa.toString(),DoubleVideoJsonRoot.class);
		System.out.println(doubleVideoJsonRoot.getFileList().get(0).getDownUrl());
		result=doubleVideoJsonRoot.getFileList().get(0).getDownUrl();
		return result;
	}
	public static String   getDoubleVideoUrl() {
	   String result="";
		TreeMap<String, Object>  paramMap = new TreeMap();
		paramMap.put("UserName", "demo@cloudroom.com");
		paramMap.put("UserPswd", Md5Util.string2MD5("123456"));
		paramMap.put("fileName", "/2019-04-29/王晓艳_删除投资基金1号2019-04-29_10-42-34.mp4");
		String postUrl ="https://shuanglu.shassel.com:2726/api/netdisk/getFiles";
		JSONObject aa = buildHttpClient(postUrl ,   paramMap);
		DoubleVideoJsonRoot doubleVideoJsonRoot= new Gson().fromJson(aa.toString(),DoubleVideoJsonRoot.class);
		System.out.println(doubleVideoJsonRoot.getFileList().get(0).getDownUrl());
		result=doubleVideoJsonRoot.getFileList().get(0).getDownUrl();
		return result;
	}
	
	public static void   main( String[] args ) {
		//System.out.println(MD5Util.md5Hex("227301"+"&"+"78678d38c9be7b60fdf6034bfe485025"));
//		long aa = System.currentTimeMillis();
//		System.out.println(aa+"");
//		
//		queryVodInfo("49730828","040b8c481aed4fb4b6855cf8128d5261","m3u8");
		//
		TreeMap<String, Object>  paramMap = new TreeMap();
		paramMap.put("UserName", "demo@cloudroom.com");
		paramMap.put("UserPswd", Md5Util.string2MD5("123456"));
		paramMap.put("fileName", "/2019-04-29/王晓艳_删除投资基金1号2019-04-29_10-42-34.mp4");
		String postUrl ="https://shuanglu.shassel.com:2726/api/netdisk/getFiles";
		JSONObject aa = buildHttpClient(postUrl ,   paramMap);
		System.out.print(aa.toString());
		DoubleVideoJsonRoot doubleVideoJsonRoot= new Gson().fromJson(aa.toString(),DoubleVideoJsonRoot.class);
		System.out.println(doubleVideoJsonRoot.getFileList().get(0).getDownUrl());
		System.out.print("key===="+aa.get("fileList"));
		//aa.get("downUrl")

	}
    
}